﻿using AutoMapper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Infrastructure.ApllicationDto;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoWcsManage;
using Zocono.WCS.Domain.EntityInfo;

namespace Zocono.WCS.Domain.PlcReadDbDomain
{
    public class PlcReadDbInfoService : IPlcReadDbInfoService
    {
        private readonly ILogUtil _logUtil;
        private readonly IPlcReadDbInfoRepository _PlcReadDbInfoRepository;
        private readonly IMapper _Mapper;
        private readonly ISnowflake _Snowflake;


        public PlcReadDbInfoService(ISnowflake snowflake, IPlcReadDbInfoRepository PlcReadDbInfoRepository, ILogUtil logUtil, IMapper mapper)
        {
            _PlcReadDbInfoRepository = PlcReadDbInfoRepository;
            _logUtil = logUtil;
            _Mapper = mapper;
            _Snowflake = snowflake;
        }

        public async Task<long> AddPlcReadDbInfoAsync(PlcReadDbInfoDto PlcReadDbInfoDto)
        {
            var PlcReadDbInfo = _Mapper.Map<PlcReadDbInfoDto, PlcReadDbInfo>(PlcReadDbInfoDto);
            long id = _Snowflake.GetId();
            PlcReadDbInfo.ID = id;
            bool result = await _PlcReadDbInfoRepository.InsertAsync(PlcReadDbInfo);
            return result ? id : 0;
        }

        public async Task<bool> DeletePlcReadDbInfoAsync(long ID)
        {
            bool result = await _PlcReadDbInfoRepository.DeleteAsync(it => it.ID == ID);
            return result;
        }

        public async Task<bool> UpdatePlcReadDbInfoAsync(PlcReadDbInfoDto PlcReadDbInfoDto)
        {
            var PlcReadDbInfo = _Mapper.Map<PlcReadDbInfoDto, PlcReadDbInfo>(PlcReadDbInfoDto);
            bool result = await _PlcReadDbInfoRepository.UpdateAsync(PlcReadDbInfo);
            return result;
        }

        public async Task<PageDto<PlcReadDbInfoDto>> GetPlcReadDbInfoAsync(QueryPlcReadDbInfoDto queryPlcReadDbInfoDto)
        {
            PageModel page = new PageModel { PageIndex = queryPlcReadDbInfoDto.PageIndex, PageSize = queryPlcReadDbInfoDto.PageSize, };
            var jobID = queryPlcReadDbInfoDto.JobID;
            var plcConnectionID = queryPlcReadDbInfoDto.PlcConnectionID;
            var dbCode = queryPlcReadDbInfoDto.DbCode;
            var isHeartbeat = queryPlcReadDbInfoDto.IsHeartbeat;
            var isEnable = queryPlcReadDbInfoDto.IsEnable;
            var processModuleType = queryPlcReadDbInfoDto.ProcessModuleType;
            var processModuleName = queryPlcReadDbInfoDto.ProcessModuleName;
            var plcName = queryPlcReadDbInfoDto.PlcName;
            var equipmentCode = queryPlcReadDbInfoDto.EquipmentCode;
            var exp = Expressionable.Create<PlcReadDbInfo, JobInfo, PlcConnectionInfo>();
            exp.AndIF(jobID.HasValue && jobID != 0, (a, b, c) => a.JobID == jobID);
            exp.AndIF(plcConnectionID.HasValue && plcConnectionID != 0, (a, b, c) => a.PlcConnectionID == plcConnectionID);
            exp.AndIF(dbCode.HasValue && dbCode != 0, (a, b, c) => a.DbCode == dbCode);
            exp.AndIF(isHeartbeat.HasValue, (a, b, c) => a.IsHeartbeat == isHeartbeat);
            exp.AndIF(isEnable.HasValue, (a, b, c) => a.IsEnable == isEnable);
            exp.AndIF(!string.IsNullOrEmpty(processModuleType), (a, b, c) => a.ProcessModuleType.Contains(processModuleType));
            exp.AndIF(!string.IsNullOrEmpty(processModuleName), (a, b, c) => a.ProcessModuleName.Contains(processModuleName));
            exp.AndIF(!string.IsNullOrEmpty(equipmentCode), (a, b, c) => a.EquipmentCode.Contains(equipmentCode));
            exp.AndIF(!string.IsNullOrEmpty(plcName), (a, b, c) => a.PlcName.Contains(plcName));

            return await _PlcReadDbInfoRepository.GetPlcReadDbInfoAsync(exp.ToExpression(), page);
        }

        public async Task<PlcReadDbInfoDto> GetPlcReadDbInfoByIDAsync(long ID)
        {
            var result = await _PlcReadDbInfoRepository.GetByIdAsync(ID);
            var vModel = _Mapper.Map<PlcReadDbInfo, PlcReadDbInfoDto>(result);
            return vModel;
        }
    }
}
